/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include <stdio.h>
#include "los_swtmr.h"
#include "time.h"
#include "los_sys.h"
#include "los_api_timer.h"
#include "los_inspect_entry.h"
#include "los_swtmr.ph"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */
#define CHECK_SWTMRID(usSwTmrID, uvIntSave, usSwTmrCBID, pstSwtmr)\
{\
    if (usSwTmrID >= OS_SWTMR_MAX_TIMERID)\
   {\
       return LOS_ERRNO_SWTMR_ID_INVALID;\
   }\
   uvIntSave = LOS_IntLock();\
   usSwTmrCBID = usSwTmrID % LOSCFG_BASE_CORE_SWTMR_LIMIT;\
   pstSwtmr = m_pstSwtmrCBArray + usSwTmrCBID;\
   if (pstSwtmr->usTimerID != usSwTmrID)\
   {\
       LOS_IntRestore(uvIntSave);\
       return LOS_ERRNO_SWTMR_ID_INVALID;\
   }\
}
static UINT32 g_timercount2 = 0;
UINT32 g_Tickcount2[10] = {0};
static VOID Timer2_Callback(UINT32 arg)
{
    UINT32 uwRet = LOS_OK;
    g_Tickcount2[g_timercount2] = (UINT32)LOS_TickCountGet();
    g_timercount2++;
}
UINT32 Example_TimerPeriodEntry(VOID)
{
    UINT16 id2;// timer id
    UINT32 uwRet = LOS_OK;
    UINT8 i;
    SWTMR_CTRL_S  *pstSwtmr;
    UINTPTR  uvIntSave;
    UINT16 usSwTmrCBID;
    UINT32 Temp_count;
    dprintf("\r\n--------4.11 Timer period test!--------\r\n");
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == YES)
    uwRet = LOS_SwtmrCreate(500, LOS_SWTMR_MODE_PERIOD, Timer2_Callback, &id2, 1, OS_SWTMR_ROUSES_ALLOW, OS_SWTMR_ALIGN_INSENSITIVE);
#else
    uwRet = LOS_SwtmrCreate(500, LOS_SWTMR_MODE_PERIOD, Timer2_Callback, &id2, 1);
#endif
    if(LOS_OK != uwRet)
    {
        dprintf("create Timer2 failed .\r\n");
    }
    else
    {
        dprintf("\r\n1. create Timer2 success! uwInterval is 500 ms.\r\n");
    }

    uwRet = LOS_SwtmrStart(id2);
    CHECK_SWTMRID(id2, uvIntSave, usSwTmrCBID, pstSwtmr);
    dprintf("\r\n1. pstSwtmr.uwCount is %d.\r\n",(*pstSwtmr).uwCount);
    if (LOS_OK != uwRet)
    {
        dprintf("start Timer2 failed .\r\n");
    }
    else
    {
        dprintf("\r\n2. start Timer2 success!\r\n");
    }
    LOS_TaskDelay(250);
    dprintf("\r\n1. pstSwtmr.uwCount delay 250 tick is %d.\r\n",(*pstSwtmr).uwCount);
    LOS_TaskDelay(2200);
    dprintf("\r\n3. LOS_TaskDelay 2200ms!\r\n");
    Temp_count=(*pstSwtmr).uwCount;
    uwRet = LOS_SwtmrStop(id2);
    dprintf("\r\n1. Temp_count before stop is %d.\r\n",Temp_count);
    if (LOS_OK != uwRet)
    {
        dprintf("stop Timer2 failed .\r\n");
    }
    else
    {
        dprintf("\r\n5. stop Timer2 success!\r\n");
    }
    dprintf("\r\n1. pstSwtmr.uwCount stop is %d.\r\n",(*pstSwtmr).uwCount);
    LOS_TaskDelay(100);
    dprintf("\r\n1. pstSwtmr.uwCount stop after 100 tick is %d.\r\n",(*pstSwtmr).uwCount);
    uwRet = LOS_SwtmrDelete(id2);
    if (LOS_OK != uwRet)
    {
        dprintf("delete Timer2 failed .\r\n");
    }
    else
    {
        dprintf("\r\n6. delete Timer2 success!\r\n");
    }
    dprintf("\r\n1. pstSwtmr.uwCount delete is %d.\r\n",(*pstSwtmr).uwCount);
    while(g_timercount2<4)
    {
        LOS_TaskDelay(100);
    }
    for(i=0;i<g_timercount2;i++)
    {
        dprintf("\r\n4. Timer2_Callback entry num=%d, g_timercount=%d .", i, g_Tickcount2[i]);
    }
    g_timercount2=0;
    return LOS_OK;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
